package application;

import application.Seed;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import login.ConnectionManager;
import login.User;
/**
 * Provides  access to database for seed related functions
 * 
 * @author Aaryn
 */
public class SeedDAO {
    
     
    static Connection currentCon = null;

        /**
         * Takes a seed object passed in from the CreateSeedServlet 
         * And stores it in the seeds table in the database
         * Also stores a row in the usersseeds lookup table to associate
         * the newly created seed with the user that created it
         * @param session the current session to know which user the seed belongs to
         * @param seed the new seed to go into the databse
         * @return Seed the new seed that was written to the database
         */
    public static Seed createSeed(Seed seed, HttpSession session) {
        Statement stmt = null;
        User currentUser = (User) session.getAttribute("currentSessionUser");
        
        if(seed.getName()==null || (seed.getName().length()<1 || seed.getName().length()>50)){
                seed.setStatus("Name must be between 1 and 50 characters");
                return seed;
        }
        if(seed.getScientificName()==null || (seed.getScientificName().length()<1 || seed.getScientificName().length()>50)){
                seed.setStatus("Scientific name must be between 1 and 50 characters");
                return seed;
        }
        if(seed.getGrowingTips()==null){
            seed.setGrowingTips("");
        }
        if(seed.getGrowingTips().length()>200){
                seed.setStatus("Growing tips must be less than 200 characters");
                return seed;
        }
        if(seed.getSpecialRequirements()==null){
            seed.setGrowingTips("");
        }
        if(seed.getSpecialRequirements().length()>200){
                seed.setStatus("Special requirements must be less than 200 characters");
                return seed;
        }
        if(seed.getHarvestingInformation()==null){
            seed.setGrowingTips("");
        }
        if(seed.getHarvestingInformation().length()>200){
                seed.setStatus("Harvesting info must be less than 200 characters");
                return seed;
        }
        if(seed.getGrowingSeason()==null || (seed.getGrowingSeason().length()<1 || seed.getGrowingSeason().length()>50)){
                seed.setStatus("Growing season must be between 1 and 50 characters");
                return seed;
        }
        if(!seed.getQuantity().matches("([0-9]+)|([0-9]+.[0-9]*)")){
                seed.setStatus("Quantity must be a number");
                return seed;
        }
        if(!seed.getPrice().matches("([0-9]+)|([0-9]+\\.[0-9]{2})")){
                seed.setStatus("Price must be to the cents or dollar");
                return seed;
        }
        if(seed.getPrice().matches("[0-9]+") && !seed.getPrice().matches("[0-9]+\\.[0-9]{2}")){
               seed.setPrice(seed.getPrice()+".00");
        }
        seed.setName(seed.getName().replaceAll("'", "''"));
        seed.setScientificName(seed.getScientificName().replaceAll("'", "''"));
        seed.setGrowingTips(seed.getGrowingTips().replaceAll("'", "''"));
        seed.setSpecialRequirements(seed.getSpecialRequirements().replaceAll("'", "''"));
        seed.setHarvestingInformation(seed.getHarvestingInformation().replaceAll("'", "''"));
        seed.setGrowingSeason(seed.getGrowingSeason().replaceAll("'", "''"));
        seed.setStatus("seed added");
        String insertQuery = "INSERT INTO seeds (name, scientificName, growingTips, specialRequirements, harvestingInformation, growingSeason, climateZone, hardiness, sellerUsername, quantity, price)"
                + " VALUES ('"+seed.getName()+"','"+seed.getScientificName()+"','"+seed.getGrowingTips()+"','"+seed.getSpecialRequirements()+"','"
                + seed.getHarvestingInformation()+"','"+seed.getGrowingSeason()+"','"+seed.getClimateZone()+"','"+seed.getHardiness()+"','"+currentUser.getUsername().replace("'","''")+"','"+Double.parseDouble(seed.getQuantity())+"','"+"$"+seed.getPrice()+"')";
        try {
            currentCon = ConnectionManager.getConnection();
            stmt=currentCon.createStatement();
            stmt.executeUpdate(insertQuery);
        }catch (Exception ex) {
            System.out.println("connection bad-probably database url" + ex);
        }
        
        return seed;
    }
}
